<#assign module="freight"/>

<@override name="header">
<link rel="stylesheet" href="${ctx}/assets/css/datetimepicker.css">
<style>
	/* autocomplete */
	div.ac {border: 1px solid #ccc;position:absolute;display: none;overflow: auto;background-color: #ffffff;z-index:3000;}
	div.ac > ul {margin-top:10px;padding:0;}
	div.ac > ul > li {height:33px;line-height:33px;cursor:pointer;}
	div.ac > ul > li:hover{background:#eee;}
	div.ac > ul > li > div span {padding-left:15px;}
	div.ac > ul > li > div span em {color:red;font-style: normal;}
</style>
</@override>
<@override name="body">
<div class="tabbable tabs-left">
	<div class="row">
		<div class="col-xs-12">
			<div class="orders-navt">
				<ul class="orders-nav">
	                <@shiro.hasPermission name='freightTruck'>
	                	<li class="orders-click"><a  href="${ctx}/freight/trucks" class="orders-click-a">车辆</a></li>
	                </@shiro.hasPermission>
	                 <@shiro.hasPermission name='freightTruck'>
	                	<li><a  href="${ctx}/freight/truckTypes">类型</a></li>
	                </@shiro.hasPermission>
	            </ul>
	    	</div>
	    	<div class="orders-navh">
	    	<#--id="addOrder" class="add"--> 
		       <button class="pull-right btn btn-primary addCar"><i class="icon-plus"></i>&nbsp;添加车辆</button>
			</div>
		</div>
	</div>
</div>

<div class="tab-content">
	<div class="container-fluid ">
  		<div class="row query-pad">
  			<div class="col-xs-11" style="padding: 0px;">
  			<@shiro.isMainCompany>	
      			<div class="col-xs-4 input-query">
      				<span>所属公司:</span>
      				<input type="text" class="form-control form-contb" id="companyName1" name="companyName1" placeholder="请输入公司名字"/>
        		</div>
        	</@shiro.isMainCompany>		
        		<div class="col-xs-4 input-query">
        			<span>货车类型:</span>
      				<input type="text" class="form-control form-contb" id="carType1" name="carType1" placeholder="请输入汽车类型"/>
        		</div>
        		<div class="col-xs-4 input-query">
        			<span>员工名字:</span>
      				<input type="text" class="form-control form-contb" id="employName1" name="employName1" placeholder="请输入驾驶员名字/电话"/>
        		</div>
    		</div>
    		<div class="col-xs-1"style="padding: 0px;">
    			<div class="input-query-bttom">
        			<button id="searchButton" type="button">查询</button>
        		</div>
    		</div>
  		</div>
  		<div class="row query-pad choose" style="display: none;">
			<div class="col-xs-12" style="padding:0px; line-height:3;">
				<div class="col-xs-4">
					<div class="input-query " style="padding:0px; line-height:3;">
        			<span>车牌号:</span>
      				<input type="text" class="form-control form-contb" id="carNumber1" name="carNumber1" placeholder="请输入车牌号"/>
        		</div>
  				</div>
				<div class="media-wit melect col-xs-4" style="padding:15px; line-height:3;">
					<span>货车状态:</span>
				    <input type="checkbox"  name="status" value="0">
				    <span class="checkbox-margin">空闲</span>
				    <input type="checkbox" name="status" value="1">
				    <span class="checkbox-margin">忙碌</span>
				    <input type="checkbox" name="status" value="2">
				    <span class="checkbox-margin">离线</span>
  				</div>
  				
  			</div>
  		</div>
  		<div class="query-jiant">
			<a href="#"><img id="jiant" src="${ctx}/assets/img/jiant2.png" /></a>          			
  		</div>
  	</div>
</div>

<!--列表-->
<div class="tabta-content">  	
	<table class="table table-condensed table-striped">
		<tbody id="car_tbody">
			<tr>
				<tr>
						<th>状态</th>
						<th>车牌号</th>
						<th>车辆名称</th>
						<@shiro.isMainCompany>
						<th>所属公司</th>
						</@shiro.isMainCompany>
						<th>货车类型</th>
						<th>驾驶员工</th>
						<th>创建时间</th>
						<th>操作</th>
					</tr>
			</tr>
		</tbody>
	</table>
	
	<ul class="pagination pull-left pagination-sm pageshow" style="display:none;">
		<li><a></a></li>
	</ul>
	<ul class="pagination pull-left pagination-sm pagedata" style="display:none;">
    </ul>
</div>



<!--添加车辆-->
<div class="form-horizontal">
	<div class="modal fade" id="dialog_car" data-backdrop="static">
	   <div class="modal-dialog">
	     <div class="modal-content">
	    	<div class="modal-header">
		        <button type="button" class="close" data-dismiss="modal" aria-hidden="false">&times;</button>
	        	<h4 class="modal-title">添加车辆</h4>
	        </div>
		    <div class="modal-body">
	      	<form action=""  method="post">
	      		<input type="hidden" id="employId" name="employId">
	      	<@shiro.isMainCompany>	
	      		<div class="form-group">
	                <label class="col-sm-3 control-label">所属公司:</label>
	                <div class="col-sm-9">
		                <select class="form-control" id="companyId" >
							<#if companys??>
								<option value="">请选择公司</option>
								<#list companys as company>
									<option value="${company.id}" <#if companyId?? && companyId==company.id>selected="selected"</#if>>${company.abbreviation?if_exists}</option>
								</#list>
							</#if>
						</select>
	                </div>
				 </div>
			</@shiro.isMainCompany>	 
                <div class="form-group">
	                <label class="col-sm-3 control-label">货车名称:</label>
	                <div class="col-sm-9">
	                	<input id="name"  class="form-control" ></input>
	                </div>
                </div>
				 <div class="form-group">
                    <label class="col-sm-3 control-label">车牌号:</label>
                    <div class="col-sm-9">
                    	<input id="carNumber"  class="form-control" placeholder="如:京A12345"></input>
                    </div>
				 </div>
			 	<div class="form-group">
					<label class="col-sm-3 control-label">货车类型:</label>
	                <div class="col-sm-9">
		                <select class="form-control" id="truckTypeId" >
							<#if truckTypes??>
								<#list truckTypes as truckType>
									<option value="${truckType.id}">${truckType.typeName?if_exists}<#if truckType.cangointotown>--入城</#if><@shiro.isMainCompany>&emsp;&emsp;&emsp;${truckType.companyName?if_exists}</@shiro.isMainCompany></option>
								</#list>
							</#if>
						</select>
	                </div>
				 </div>
			 	<div class="form-group">
				 	<label class="col-sm-3 control-label">指派服务人员:</label>
	                <div id="text_div" class="col-sm-9">
						  <input class="form-control" id="employName" name="employName" placeholder="输入:工号、电话、姓名"  type="text"/>
	                </div>
				 </div>
				 <div class="form-group">
                 	  <div class="col-sm-5">
                	  </div>
                	  <div class="col-sm-3">
					      <input  type="button" class="btn btn-primary" id="save" value="确定">
					      <button type="button" class="btn btn-default" id="close_modal">关闭</button>
		        	  </div>
		          </div>
		  	</form>
	     	</div>
	     </div>
	  </div>
	</div>
</div>
<!--修改车辆信息-->
<div class="form-horizontal">
	<div class="modal fade" id="dialog_car_update" data-backdrop="static">
	   <div class="modal-dialog">
	     <div class="modal-content">
	    	<div class="modal-header">
		        <button type="button" class="close" data-dismiss="modal" aria-hidden="false">&times;</button>
	        	<h4 class="modal-title">修改车辆信息</h4>
	        </div>
		    <div class="modal-body">
	      	<form action=""  method="post">
	      		<input type="hidden" id="carId">
	      		<input type="hidden" id="employId2">
	      		<@shiro.isMainCompany>
	      		<div class="form-group">
	                <label class="col-sm-3 control-label">所属公司:</label>
	                <div class="col-sm-9">
		                <select class="form-control" id="companyId2" >
							<#if companys??>
								<option value="">请选择公司</option>
								<#list companys as company>
									<option value="${company.id}" <#if companyId?? && companyId==company.id>selected="selected"</#if>>${company.abbreviation?if_exists}</option>
								</#list>
							</#if>
						</select>
	                </div>
				 </div>
				</@shiro.isMainCompany> 
                <div class="form-group">
	                <label class="col-sm-3 control-label">货车名称:</label>
	                <div class="col-sm-9">
	                	<input id="name2"  class="form-control" ></input>
	                </div>
                </div>
				 <div class="form-group">
                    <label class="col-sm-3 control-label">车牌号:</label>
                    <div class="col-sm-9">
                    	<input id="carNumber2"  class="form-control" placeholder="如:京A12345"></input>
                    </div>
				 </div>
			 	<div class="form-group">
					<label class="col-sm-3 control-label">货车类型:</label>
	                <div class="col-sm-9">
		                <select class="form-control" id="truckTypeId2" >
							<#if truckTypes??>
								<#list truckTypes as truckType>
									<option value="${truckType.id}">${truckType.typeName?if_exists}<#if truckType.cangointotown>-入城</#if><@shiro.isMainCompany>&emsp;&emsp;&emsp;${truckType.companyName?if_exists}</@shiro.isMainCompany></option>
								</#list>
							</#if>
						</select>
	                </div>
				 </div>
				 <div class="form-group">
	                <label class="col-sm-3 control-label">状态:</label>
	                <div class="col-sm-9">
		                <select class="form-control" id="status" >
							<option value="0">空闲</option>
							<option value="1">忙碌</option>
							<option value="2">离线</option>
						</select>
	                </div>
				 </div>
		         
			 	<div class="form-group">
				 	<label class="col-sm-3 control-label">服务人员司机:</label>
	                <div id="text_div" class="col-sm-9">
						  <input class="form-control" id="employName2"  placeholder="输入:工号、电话、姓名"  type="text"/>
	                </div>
				 </div>
				 <div class="form-group">
                 	  <div class="col-sm-5">
                	  </div>
                	  <div class="col-sm-3">
					      <input  type="button" class="btn btn-primary" id="update" value="保存">
					      <button type="button" class="btn btn-default" id="close_modal_update">取消</button>
		        	  </div>
		          </div>
		  	</form>
	     	</div>
	     </div>
	  </div>
	</div>
</div>
</@override>
<@override name="footer">
<script type="text/javascript">
  var isMainCompany=false;
 <@shiro.isMainCompany>
      isMainCompany=true;
 </@shiro.isMainCompany>
</script>
<script src="${ctx}/assets/js/bootstrap-datetimepicker.min.js"></script>
<script src="${ctx}/assets/js/bootstrap-datetimepicker.zh-CN.js"></script>
<script src="${ctx}/assets/js/freight/car.js">
<script src="${ctx}/assets/js/ajaxfileupload.js"></script>
<script src="${ctx}/assets/js/jquery.autocomplete.min.js"></script>
<script src="${ctx}/assets/js/jquery.paging.min.js"></script>
<script type="text/javascript">
	var thisButton;
	function deleteTruck() {
	    $.get('${ctx}/freight/delete',{'id':thisButton.attr("data-value")},function(data){
	            if(data.success){
	            	thisButton.parent().parent().remove();
	                $.scojs_message('车辆删除成功', $.scojs_message.TYPE_OK);
		                setTimeout(function(){
							window.location.reload(true);
						}, 800);
	            } else {
	                $.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
	            }
	    },'json');
	}
	
	$(document).ready(function(){
	    $("#carNumber").change(function(){
           if($(this).val()!=""){
              $.get('${ctx}/freight/truckData',{'carNumber':$(this).val()},function(data){
                if(data.totalElements>0){
                     $.scojs_message("【"+$("#carNumber").val()+"】车牌号已存在", $.scojs_message.TYPE_ERROR);
                     $("#carNumber").val("");
                }
            
            },'json');	    
           }
	    });
	
	 $("#carNumber2").change(function(){
	 
	        if($(this).val()!=""){
	          $.get('${ctx}/freight/truckData',{'carNumber':$(this).val()},function(data){
                if(data.totalElements>0){
                     $.scojs_message("【"+$("#carNumber2").val()+"】车牌号已存在", $.scojs_message.TYPE_ERROR);
                     $("#carNumber2").val("");
                }
            
            },'json');	  
	        
	        }
              
	    });
	
	
		<@shiro.isMainCompany>
	    var company_id=$("#companyId").val();
	    	$("#truckTypeId").html("");
	    	if(company_id==""){
	    		var text="<option value=''>请先选择公司</option>";
		        $("#truckTypeId").append(text);
	    	}else{
	    	     $.get('${ctx}/freight/findByTruckTypeCompanyId',{'companyId':company_id},function(data){
	    		if(data!=null&&data.truckTypes!=null&&data.truckTypes.length>0){
	    			$(data.truckTypes).each(function(index,element){
						var id=data.truckTypes[index].id;
						var typeName=data.truckTypes[index].typeName;
		            	var company_name="";
						if(company_id==data.companyId_main){
							company_name="&emsp;&emsp;&emsp;&emsp;"+data.truckTypes[index].companyName;
						}
		            	var text="<option value="+id+">"+typeName+(data.truckTypes[index].cangointotown?"--入城":"")+company_name+"</option>";
		            	$("#truckTypeId").append(text);
					 });
	    		}else{
	    		    $.scojs_message("该公司没有货车类型", $.scojs_message.TYPE_ERROR);
	    		}
	    	
	    	},'json');
	    	
	    	}
	    </@shiro.isMainCompany>
	    $(document).on("click",".deletethis",function(){
	          thisButton = $(this);
	          var confirm = $.scojs_confirm({
	            content: "你真的要删除该车辆吗？",
	            action:deleteTruck
	          });
	          confirm.show();
	    });
		$('#employName').AutoComplete({
	        'data': "${ctx}/freight/getDriver",
			ajaxDataType: 'json',
			ajaxParams:function(keyword){
				return {'param':$('#employName').val()};
			},
	        'width': 'auto',
	        'async': false,
	        'listStyle': 'custom',
	        'emphasis': false,
	        'matchHandler': function(keyword, data){
	            return true;
	        },
	        'createItemHandler': function(index, data){
	        	return "<span>"+data.username+"    "+data.phone+"</span>";
	        },
	        'afterSelectedHandler': function(data){
	        	$('#employName').val(data.username+"    "+data.phone);
	        	$('#employId').val(data.id);
	        }
	    }).AutoComplete('show');
			
	//添加车辆
	    $(".addCar").click(function(){
	    	$('#dialog_car').modal();
	    });
	    //公司修改后对应的车辆类型也修改
	    $("#companyId").change(function(){
	    	var company_id=$("#companyId").val();
	    	$("#truckTypeId").html("");
	    	if(company_id==""){
	    		var text="<option value=''>请先选择公司</option>";
		        $("#truckTypeId").append(text);
		        return false;
	    	}
	    	$.get('${ctx}/freight/findByTruckTypeCompanyId',{'companyId':company_id},function(data){
	    		if(data!=null&&data.truckTypes!=null&&data.truckTypes.length>0){
	    			$(data.truckTypes).each(function(index,element){
						var id=data.truckTypes[index].id;
						var typeName=data.truckTypes[index].typeName;
		            	var company_name="";
						if(company_id==data.companyId_main){
							company_name="&emsp;&emsp;&emsp;&emsp;"+data.truckTypes[index].companyName;
						}
		            	var text="<option value="+id+">"+typeName+(data.truckTypes[index].cangointotown?"--入城":"")+company_name+"</option>";
		            	$("#truckTypeId").append(text);
					 });
	    		}else{
	    		    $.scojs_message("该公司没有货车类型", $.scojs_message.TYPE_ERROR);
	    		}
	    	
	    	},'json');
	    });
   		$("#save").click(function(){
	     		var companyId=$('#companyId').val();
	     		var companyName=$('#companyId').find("option:selected").text();
	     		var name=$('#name').val();
	     		var carNumber=$('#carNumber').val();
	     		var truckTypeName = $('#truckTypeId').find("option:selected").text().split("--")[0];
	     	<@shiro.isMainCompany>
	     		if(!companyId){
	     			$.scojs_message("请选择公司", $.scojs_message.TYPE_ERROR);
	     			return false;
	     		}
	     	</@shiro.isMainCompany>
	     		if($('#truckTypeId').val()==""){
	     	        $.scojs_message("请选择货车类型", $.scojs_message.TYPE_ERROR);
	     			return false;
	     	    }
	     		if(name==""){
	     			$.scojs_message("请输入车辆名称", $.scojs_message.TYPE_ERROR);
	     			return false;
	     		}
	     		if(!carNumber.trim()){
	     			$.scojs_message("请输入车牌号", $.scojs_message.TYPE_ERROR);
	     			return false;
	     		}else {
	     			if(!(/^[\u4e00-\u9fa5]{1}[A-z]{1}[A-z_0-9]{5}$/.test(carNumber))){
		     			$.scojs_message("车牌号格式不正确", $.scojs_message.TYPE_ERROR);
		     			return false;
	     			}
	     		}
	     		
	     		if(($('#employName').val()!=null&&$('#employName').val()!="")&&($('#employId').val()==null|| $('#employId').val() == "")){
	     		   $.scojs_message("所选服务人员不存在", $.scojs_message.TYPE_ERROR);
		     			return false;
	     		}
	     		
	     		$('#dialog_car').modal("hide");
	     		
				$.post('${ctx}/freight/createTruck',
				{'companyId':companyId,'companyName':companyName,'name':name,
				'carNumber':carNumber,'truckTypeName':truckTypeName,'truckTypeId':$('#truckTypeId').val(),'status':$('#status').val(),'employId':$('#employId').val(),'employName':$('#employName').val()},
				function(data){
		           if(data.success){ 
		                $.scojs_message("创建成功", $.scojs_message.TYPE_OK);
		                setTimeout(function(){
							window.location.reload(true);
						}, 800);
			       }else{
		                $.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
		                setTimeout(function(){
							window.location.reload(true);
						}, 800);
			       }
			    },'json');
	     });
	    $("#close_modal").click(function(){
			$('#dialog_car').modal("hide");
		});
		var tempTruckTypeId=null;
		
		//修改车辆
	    $(document).on("click",".updatethis",function(){
	    	var carId=$(this).attr("data-value");
			$("#carId").val(carId);
			$.ajax({
             type: "GET",
             url: "${ctx}/freight/queryTruck",
             data: {id:carId},	
             dataType: "json",
             success: function(object){ 
             			var car=object.data;
             			$("#name2").val(car.name);
             			$("#companyId2").val(car.companyId);
             			$("#carNumber2").val(car.carNumber);
             			$("#truckTypeId2").val(car.truckTypeId);
             			tempTruckTypeId=car.truckTypeId;
             			$("#employName2").val(car.driverName);
             			$("#employId2").val(car.driverId);
             			$("#status").val(car.status);
             			$("#companyId2").change();
			    	 }
	         });
         
	    	$('#dialog_car_update').modal();
	    });
	    //公司修改后对应的车辆类型也修改
	    $("#companyId2").change(function(){
	    	var company_id=$("#companyId2").val();
	    	$("#truckTypeId2").html("");
	    	if(company_id==""){
	    		var text="<option value=''>请先选择公司</option>";
		        $("#truckTypeId2").append(text);
		        return false;
	    	}
	    	if(company_id!=""){
	    		$.get('${ctx}/freight/findByTruckTypeCompanyId',{'companyId':company_id},function(data){
		    		if(data!=null&&data.truckTypes!=null&&data.truckTypes.length>0){
		    			$(data.truckTypes).each(function(index,element){
							var id=data.truckTypes[index].id;
						var typeName=data.truckTypes[index].typeName;
		            	var company_name="";
						if(company_id==data.companyId_main){
							company_name="&emsp;&emsp;&emsp;&emsp;"+data.truckTypes[index].companyName;
						}
		            	var text="<option "+(id==tempTruckTypeId?"selected=selected":"")+"  value="+id+">"+typeName+(data.truckTypes[index].cangointotown?"--入城":"")+company_name+"</option>";
		            	$("#truckTypeId2").append(text);
						 });
		    		}else{
		    		    $.scojs_message("该公司没有货车类型", $.scojs_message.TYPE_ERROR);
		    		    
		    		}
	    	},'json');
	    	
	    	
	    	 }
	    });
	    $("#update").click(function(){
	     		var companyId=$('#companyId2').val();
	     		var companyName=$('#companyId2').find("option:selected").text();
	     		var name=$('#name2').val();
	     		var carNumber=$('#carNumber2').val();
	     	<@shiro.isMainCompany>
	     		if(!companyId){
	     			$.scojs_message("请选择公司", $.scojs_message.TYPE_ERROR);
	     			return false;
	     		}
	     	 </@shiro.isMainCompany>
	     	    if($('#truckTypeId2').val()=="" || $('#truckTypeId2').val() == null || $('#truckTypeId2').val() == undefined){
	     	        $.scojs_message("请选择货车类型", $.scojs_message.TYPE_ERROR);
	     			return false;
	     	    }	
	     		if(name==""){
	     			$.scojs_message("请输入车辆名称", $.scojs_message.TYPE_ERROR);
	     			return false;
	     		}
	     		if(!carNumber.trim()){
	     			$.scojs_message("请输入车牌号", $.scojs_message.TYPE_ERROR);
	     			return false;
	     		}else {
	     			if(!(/^[\u4e00-\u9fa5]{1}[A-z]{1}[A-z_0-9]{5}$/.test(carNumber))){
		     			$.scojs_message("车牌号格式不正确", $.scojs_message.TYPE_ERROR);
		     			return false;
	     			}
	     		}
	     		if(($('#employName2').val()!=null&&$('#employName2').val()!="")&&($('#employId2').val()==null|| $('#employId2').val() == "")){
	     		   $.scojs_message("所选服务人员不存在", $.scojs_message.TYPE_ERROR);
		     			return false;
	     		}
				$.post('${ctx}/freight/updateTruck',
				{'id':$("#carId").val(),'companyId':companyId,'companyName':companyName,'name':name,
				'carNumber':carNumber,'truckTypeId':$('#truckTypeId2').val(),'status':$('#status').val(),'employId':$('#employId2').val(),'employName':$('#employName2').val(),},
				function(data){
		           if(data.success){ 
		          		$('#dialog_car_update').modal("hide");
		                $.scojs_message(data.message, $.scojs_message.TYPE_OK);
		                setTimeout(function(){
							window.location.reload(true);
						}, 500);
			       }else{
			      		$('#dialog_car_update').modal("hide");
		                $.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
		                setTimeout(function(){
							window.location.reload(true);
						}, 1000);
			       }
			    },'json');
	     });
	     
	    $("#close_modal_update").click(function(){
			$('#dialog_car_update').modal("hide");
		});
		
		$('#employName2').AutoComplete({
	        'data': "${ctx}/freight/getDriver",
			ajaxDataType: 'json',
			ajaxParams:function(keyword){
			    $('#employId2').val("");
				return {'param':$('#employName2').val()};
			},
	        'width': 'auto',
	        'async': false,
	        'listStyle': 'custom',
	        'emphasis': false,
	        'matchHandler': function(keyword, data){
	            return true;
	        },
	        'createItemHandler': function(index, data){
	        	return "<span>"+data.username+"    "+data.phone+"</span>";
	        },
	        'afterSelectedHandler': function(data){
	        	$('#employName2').val(data.username+"    "+data.phone);
	        	$('#employId2').val(data.id);
	        }
	    }).AutoComplete('show');
	});
 
	//<span class="label label-danger status-td">异</span>
	//<span class="label label-success status-td baoxian" title="baodan1234#PA145231811900192">保</span>
</script>
</@override>
<@extends name="../layout.ftl"/>
